package com.ljf.filter;

import com.ljf.exception.BusinessException;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@Component
public class LjfFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("开始过滤");
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        System.out.println("对request进行过滤");
        Cookie[] cookies = request.getCookies();

        if (cookies != null && cookies.length != 0){
            for (Cookie cookie: cookies){
                if (cookie.getName().equals("token")){
                    String token = cookie.getValue();
                   if (token == null){
                       throw new BusinessException("登录");
                   }
                    }

                    }
                }
        //下面这行代码就是放行
        filterChain.doFilter(servletRequest,servletResponse);


    }
}
